﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdateModelCatServiceCommand : ProviderQueueCommand<IProviderFactory, ModelCatRecord, ResponseRecord>
    {
        public UpdateModelCatServiceCommand(IProviderFactory provider) : base(provider) { }
        protected override void Validate(ModelCatRecord record)
        {
            if (record == null) throw new ArgumentNullException("request");
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var modelCat = Record;
            var obj = new ModelCat()
            {
                ActiveCode = modelCat.ActiveCode,
                AfCode = modelCat.AfCode,
                AuCode = modelCat.AuCode,
                CreateDate = modelCat.CreateDate,
                EnableFlag = modelCat.EnableFlag.HasValue ? modelCat.EnableFlag.Value.ToString() : null,
                Eom = modelCat.Eom,
                IfDate = modelCat.IfDate,
                LastUpdateDate = DateTime.Now,
                Mkt = modelCat.Mkt,
                Model = modelCat.Model,
                ModelSpec = modelCat.ModelSpec,
                MpModel = modelCat.ModelSpec,
                MpSuffix = modelCat.MpSuffix,
                ProdL1 = modelCat.ProdL1,
                ProdL2 = modelCat.ProdL2,
                ProdL3 = modelCat.ProdL3,
                ProdType = modelCat.ProdType,
                PurType = modelCat.PurType,
                SaleFlag = modelCat.SaleFlag.HasValue ? modelCat.SaleFlag.Value.ToBooleanString() : null,
                SoTransferDate = DateTime.Now,
                SoTransferFlag = BooleanString.TRUE,
                Suffix = modelCat.Suffix,
                TentativeFlag = modelCat.TentativeFlag.HasValue ? modelCat.TentativeFlag.Value.ToBooleanString() : null,
                Unit = modelCat.Unit,
            };
            var itemCheck = Providers.ModelCat.RetrieveModel(Record.Model);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.ModelCat.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.ModelCat.Update(obj);
            }
            return true;
        }
    }
}
